package com.cskaoyan.order.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.OrderDetail;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @program: duolaimall
 * @description:
 * @author: Lexis
 * @create: 2022-05-20 20:07
 **/
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter orderConverter;

    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        OrderListResponse response = new OrderListResponse();

        try {
            request.requestCheck();
            // 页码
            Integer page = request.getPage();
            // 数据量
            Integer limit = request.getSize();
            // 1升序 -1降序
            String sort = request.getSort();
            //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
            PageHelper.startPage(page, limit);
            Example example = new Example(Order.class);
            // if (sort != null) {
            //     if (sort.equals("-1")) {
            //         example.setOrderByClause("price DESC");
            //     } else if (sort.equals("1")) {
            //         example.setOrderByClause("price");
            //     }
            // }
            example.setOrderByClause("create_time DESC");
            example.createCriteria().andEqualTo("userId", request.getUserId());
            List<Order> orderList = orderMapper.selectByExample(example);
            PageInfo<Order> pageInfo = new PageInfo<>(orderList);
            long total = pageInfo.getTotal();

            List<OrderDetailInfo> orderDetailInfos = orderConverter.orderList2detailList(orderList);
            for (OrderDetailInfo detailInfo : orderDetailInfos) {
                Example orderItemExample = new Example(OrderItem.class);
                orderItemExample.setOrderByClause("create_time DESC");
                orderItemExample.createCriteria().andEqualTo("orderId", detailInfo.getOrderId());
                List<OrderItem> orderItems = orderItemMapper.selectByExample(orderItemExample);
                List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
                detailInfo.setOrderItemDto(orderItemDtos);

                OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(detailInfo.getOrderId());
                OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
                detailInfo.setOrderShippingDto(orderShippingDto);
            }
            response.setData(orderDetailInfos);
            response.setTotal(total);
            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }

        return response;
    }

    @Override
    public OrderDetailResponse orderDetail(OrderDetailRequest request) {
        return null;
    }

    @Override
    public GetOrderDetailResponse getOrderDetail(OrderDetailRequest request) {

        GetOrderDetailResponse response = new GetOrderDetailResponse();
        OrderDetail getOrderDetailDto = new OrderDetail();

        try {
            request.requestCheck();
            Order order = orderMapper.selectByPrimaryKey(request.getOrderId());

            Example example = new Example(OrderItem.class);
            example.setOrderByClause("create_time DESC");
            example.createCriteria().andEqualTo("orderId", request.getOrderId());
            List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);

            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(request.getOrderId());

            getOrderDetailDto.setOrderStatus(order.getStatus());
            getOrderDetailDto.setUserId(order.getUserId());
            getOrderDetailDto.setUserName(order.getBuyerNick());
            getOrderDetailDto.setGoodsList(orderItemDtos);
            getOrderDetailDto.setOrderTotal(order.getPayment());
            getOrderDetailDto.setTel(orderShipping.getReceiverPhone());
            getOrderDetailDto.setStreetName(orderShipping.getReceiverAddress());

            response.setGetOrderDetailDto(getOrderDetailDto);
            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }

        return response;
    }
}
